<?php
/**
 * Author: FoxxxZhu
 * Date: 2016/9/21
 * Time: 17:20
 */

namespace Forxx\Api;

class Format {

    public static function show_date($time,$default ="未知")
    {
        if(is_numeric($time))
        {
            $time = intval($time);
        }
        else
        {
            $time = strtotime($time);
        }
        if($time>0)
        {
            return date("Y-m-d H:i:s",$time);
        }
        return $default;
    }

    /**
     * 显示时长
     * @param $seconds
     * @param string $default
     * @return string
     */
    public static function show_time_long($seconds,$default = "")
    {
        $seconds = intval($seconds);
        if($seconds>0)
        {
            $str = "";
            if($seconds > 3600)
            {
                $str .= floor($seconds / 3600)."小时";
                $seconds = intval($seconds%3600);

            }
            if($seconds > 60)
            {
                $str .= floor($seconds / 60)."分";
                $seconds = intval($seconds%60);
            }
            $str .= "{$seconds}秒";
            return $str;
        }
        return $default;
    }

    /**
     * @param $str
     * @param int $length
     * @param string $replace
     * @param string $charset
     * @return string
     */
    public static function show_word_length($str,$length = 18,$replace = "...",$charset = "UTF-8")
    {
        if(empty($charset))
        {
            $charset ="UTF-8";
        }
        mb_internal_encoding($charset);
        $input_length  = mb_strlen($str);
        if($input_length <= $length )
        {
            return $str;
        }
        $mystring = mb_substr($str,0,$length);
        return $mystring."...";
    }

    /**
     * @param $str
     * @param string $input_defult_price
     * @param string $output
     * @return int|string
     */
    public static function format_input_file_size($str, $input_defult_price = 'MB', $output = 'KB')
    {
        $str = trim(strtoupper($str));
        $length = strlen($str);
        $pos = $length - 1;
        if (is_numeric($str{$pos})) {
            $str = $str . $input_defult_price;
        }
        $bytes = self::get_byte($str);
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
        $index = array_search($output, $units);

        if ($index === false) {
            return $bytes;
        } else {
            return bcdiv($bytes, bcpow(1024, $index, 0), 0);
        }
    }

    /**
     * @param $str
     * @return int|string
     */
    public static function get_byte($str)
    {
        $str = str_replace(' ', '', $str);
        $str = trim(strtoupper($str));
        $str = rtrim($str, 'Bb.');
        $pattern = "/^(\d+(\.\d+)?)([KMGT]?)$/";
        $match = [];
        preg_match($pattern, $str, $match);
        if (count($match) != 4) {
            return 0;
        } else {
            $int_num = bcmul($match[1], 1, 0);
            $float_str = substr($match[2], 1, 2);
            if (empty($float_str)) {
                $float_num = 0;
            } else {
                $float_num = intval(str_pad($float_str, 2, "0", STR_PAD_RIGHT));
            }
            $unit = $match[3];
            $res = 0;
            switch ($unit) {
                case "":
                    $res = $int_num;
                    break;
                case "K":
                    $res = bcadd(bcmul($int_num, 1024), intval($float_num * 10.24), 0);
                    break;
                case "M":
                    $res = bcmul(bcadd(bcmul($int_num, 1024), intval($float_num * 10.24), 0), 1024, 0);
                    break;
                case "G":
                    $res = bcmul(bcadd(bcmul($int_num, 1024), intval($float_num * 10.24), 0), 1024 * 1024, 0);
                    break;
                case "T":
                    $res = bcmul(bcadd(bcmul($int_num, 1024), intval($float_num * 10.24), 0), 1024 * 1024 * 1024, 0);
                    break;
            }
            return $res;
        }
    }

    /**
     * @param $byte
     * @return string
     */
    public static function format_show_file_size($byte)
    {
        //加上默认单位KB
        if(is_numeric($byte))
        {
            $byte .= "KB";
        }
        if (is_string($byte)) {
            $byte = self::get_byte($byte);
        }
        $num = $byte;
        $index = 0;
        do {
            $num = bcdiv($num, 1024, 2);
            $index++;
        } while (floatval($num) > 1024 && $index <= 4);
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
        return $num . $units[$index];
    }
}